<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="生成报表年月" prop="bbny" label-width="100px">
        <el-date-picker :editable="false" clearable size="small" v-model="queryParams.bbny" type="month" placeholder="生成报表年月" value-format="yyyy-MM">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['analysis_maintain:analysis_maintain:add']">生成</el-button>
      </el-form-item>

      <el-form-item label="报表年月" prop="bbny">
        <el-date-picker :editable="false" clearable size="small" v-model="queryParams.bbny" type="monthrange" range-separator="至" start-placeholder="开始月份" end-placeholder="结束月份" value-format="yyyy-MM">
        </el-date-picker>
      </el-form-item>
      <!--  <el-form-item label="报表编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入报表编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报表名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入报表名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['analysis_maintain:analysis_maintain:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['analysis_maintain:analysis_maintain:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['analysis_maintain:analysis_maintain:remove']">删除</el-button>
      </el-col>
      <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['analysis_maintain:analysis_maintain:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="analysis_maintainList" @selection-change="handleSelectionChange" max-height="500">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="报表年月" align="center" prop="bbny" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bbny, "{y}-{m}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="报表编号" align="center" prop="code" />
      <el-table-column label="报表名称" align="center" prop="name" />
      <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['analysis_maintain:analysis_maintain:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['analysis_maintain:analysis_maintain:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改仰口达意隆维修费用对话框 -->
    <el-dialog fullscreen :title="title" :visible.sync="open" width="1200px" append-to-body v-myDialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="报表年月" prop="bbny">
              <el-date-picker :editable="false" clearable size="small" v-model="form.bbny" type="month" value-format="yyyy-MM" placeholder="选择报表年月" :disabled="openDisabled">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="报表编号" prop="code">
              <el-input
                v-model="form.code"
                placeholder="请输入报表编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报表名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入报表名称"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
        <!-- <el-divider content-position="center"
          >仰口达意隆维修费用子信息</el-divider
        > -->
        <!-- <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddRptAnalysisMaintainSub"
              :disabled="openDisabled"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteRptAnalysisMaintainSub"
              :disabled="openDisabled"
              >删除</el-button
            >
          </el-col>
        </el-row> -->
        <el-table :data="rptAnalysisMaintainSubList" :row-class-name="rowRptAnalysisMaintainSubIndex" @selection-change="handleRptAnalysisMaintainSubSelectionChange" ref="rptAnalysisMaintainSub">
          <!-- <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          /> -->
          <el-table-column label="报表月份" prop="month">
            <template slot-scope="scope">
              <el-input v-model="scope.row.month" placeholder="请输入报表月份" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="达意隆线产量(标准箱)" prop="dylcl">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dylcl" placeholder="请输入达意隆线产量(标准箱)" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="达意隆线维修费(元)" prop="dylwxf">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dylwxf" placeholder="请输入达意隆线维修费(元)" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="达意隆线单箱维修费(元)" prop="dyldxwxf">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dyldxwxf" placeholder="请输入达意隆线单箱维修费(元)" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <br /><br />
      <!-- 图表 -->
      <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
      <el-row :gutter="30">
        <el-col :span="8" :offset="4">
          <div ref="main2" style="width: 370px; height: 300px"></div>
        </el-col>
        <el-col :span="8">
          <div ref="main3" style="width: 370px; height: 300px"></div>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAnalysis_maintain,
  getAnalysis_maintain,
  delAnalysis_maintain,
  addAnalysis_maintain,
  updateAnalysis_maintain,
} from "@/api/report/analysis/analysis_maintain";

export default {
  name: "Analysis_maintain",
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRptAnalysisMaintainSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仰口达意隆维修费用表格数据
      analysis_maintainList: [],
      // 仰口达意隆维修费用子表格数据
      rptAnalysisMaintainSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        bbny: null,
        bbny1: null,
        code: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /* 初始化echarts实例  */
    initChart(res) {
      const arr = res.data.rptAnalysisMaintainSubList;
      // console.log(arr);
      this.$nextTick(() => {
        //基于准备好的dom，初始化echarts实例
        let myChart2 = this.$echarts.init(this.$refs.main2);
        let myChart3 = this.$echarts.init(this.$refs.main3);

        //  中间对option数据进行处理
        let option2 = {
          backgroundColor: "#407EB4",
          title: {
            text: "达意隆线维修费(元)",
          },
          xAxis: {
            type: "category",
            data: [arr[0].month, arr[3].month],
          },
          yAxis: {
            type: "value",
          },
          series: [
            {
              data: [
                {
                  value: arr[0].dylwxf,
                  itemStyle: {
                    color: "#08D66F",
                  },
                },
                arr[3].dylwxf,
              ],
              itemStyle: {
                //上方显示数值
                normal: {
                  label: {
                    show: true, //开启显示
                    position: "top", //在上方显示
                    textStyle: {
                      //数值样式
                      color: "black",
                      fontSize: 16,
                    },
                  },
                },
              },
              type: "bar",
            },
          ],
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart2.setOption(option2);

        //  中间对option数据进行处理
        let option3 = {
          backgroundColor: "#407EB4",
          title: {
            text: "达意隆线单箱维修费(元)",
          },
          xAxis: {
            type: "category",
            data: [arr[0].month, arr[3].month],
          },
          yAxis: {
            type: "value",
          },
          series: [
            {
              data: [
                {
                  value: arr[0].dyldxwxf,
                  itemStyle: {
                    color: "#08D66F",
                  },
                },
                arr[3].dyldxwxf,
              ],
              itemStyle: {
                //上方显示数值
                normal: {
                  label: {
                    show: true, //开启显示
                    position: "top", //在上方显示
                    textStyle: {
                      //数值样式
                      color: "black",
                      fontSize: 16,
                    },
                  },
                },
              },
              type: "bar",
            },
          ],
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart3.setOption(option3);
      });
    },
    /** 查询仰口达意隆维修费用列表 */
    getList() {
      this.loading = true;
      listAnalysis_maintain(this.queryParams).then((response) => {
        this.analysis_maintainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bbny: null,
        code: null,
        name: null,
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.rptAnalysisMaintainSubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.bbny != null) {
        let arr = this.queryParams.bbny;
        this.queryParams.bbny = arr[0];
        this.queryParams.bbny1 = arr[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // this.openDisabled = false;
      // this.open = true;
      // this.title = "添加仰口达意隆维修费用";
      if (this.queryParams.bbny == null || this.queryParams.bbny == "") {
        this.msgError("请输入生产报表年月");
      } else if (Array.isArray(this.queryParams.bbny)) {
        this.msgError("请重置筛选日期");
      } else {
        addAnalysis_maintain(this.queryParams).then((response) => {
          this.msgSuccess("新增成功");
          // this.open = false;
          // rLoading.close();
          this.resetQuery();
          this.getList();
        });
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getAnalysis_maintain(id).then((response) => {
        this.form = response.data;
        this.rptAnalysisMaintainSubList =
          response.data.rptAnalysisMaintainSubList;
        this.initChart(response);
        this.open = true;
        this.title = "修改仰口达意隆维修费用";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getAnalysis_maintain(id).then((response) => {
        this.form = response.data;
        this.rptAnalysisMaintainSubList =
          response.data.rptAnalysisMaintainSubList;
        this.initChart(response);
        this.open = true;
        this.title = "查看仰口达意隆维修费用";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.rptAnalysisMaintainSubList =
            this.rptAnalysisMaintainSubList;
          if (this.form.id != null) {
            updateAnalysis_maintain(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addAnalysis_maintain(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除仰口达意隆维修费用编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delAnalysis_maintain(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 仰口达意隆维修费用子序号 */
    rowRptAnalysisMaintainSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 仰口达意隆维修费用子添加按钮操作 */
    handleAddRptAnalysisMaintainSub() {
      let obj = {};
      obj.month = "";
      obj.dylcl = "";
      obj.dylwxf = "";
      obj.dyldxwxf = "";
      obj.remark = "";
      this.rptAnalysisMaintainSubList.push(obj);
    },
    /** 仰口达意隆维修费用子删除按钮操作 */
    handleDeleteRptAnalysisMaintainSub() {
      if (this.checkedRptAnalysisMaintainSub.length == 0) {
        this.$alert("请先选择要删除的仰口达意隆维修费用子数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.rptAnalysisMaintainSubList.splice(
          this.checkedRptAnalysisMaintainSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleRptAnalysisMaintainSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.rptAnalysisMaintainSub.clearSelection();
        this.$refs.rptAnalysisMaintainSub.toggleRowSelection(selection.pop());
      } else {
        this.checkedRptAnalysisMaintainSub = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "analysis_maintain/analysis_maintain/export",
        {
          ...this.queryParams,
        },
        "仰口达意隆维修费用" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
